# 机器学习进阶-图像形态学操作-梯度运算 cv2.GRADIENT(梯度运算-膨胀图像-腐蚀后的图像)
# https://www.cnblogs.com/my-love-is-python/p/10395127.html
import cv2
import matplotlib.pyplot as plt
import numpy as np

# 1.读取图片
pie = cv2.imread('pie.png')

# 2. 绘制腐蚀和膨胀图片
kernel = np.ones((5, 5), np.uint8)
erosion = cv2.erode(pie, kernel, iterations=2)
dilate = cv2.erode(pie, kernel, iterations=2)

# 3. 相减获得对应的图片
decrease = dilate - erosion
print(decrease.shape)

# 4. 使用cv2.MORPH_GRADIENT获得梯度运算的图片
gradient = cv2.morphologyEx(pie, cv2.MORPH_GRADIENT, kernel, iterations=2)

# 5. 绘制两者的图片
images = [pie, decrease, gradient]
titles = ['PIE', 'DECREASE', 'GRADIENT']

for i in range(3):
    plt.subplot(2, 3, i + 1)
    plt.imshow(images[i])
    plt.title(titles[i])
plt.show()